/**
 * 
 */
package com.neal.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.neal.domain.enums.ResourceType;
import com.neal.domain.securities.Resources;
import com.neal.domain.securities.Role;
import com.neal.domain.securities.User;
import com.neal.repositories.securities.RoleRepository;
import com.neal.repositories.securities.UserRepository;

/**
 * @author Neal
 * @version 1.0
 * @description	基础测试
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/beans.xml" })
public class GenerateDate {
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private RoleRepository roleRepository; 
	
	@Test
	public void run(){
		System.out.println("run success!");
	}
	
	@Test
	public void generateTestData(){
		
		List<Resources> resources = new ArrayList<Resources>();
		
		Resources r1 = new Resources();
		r1.setName("Message_Manage"); r1.setPriority(1); r1.setResourceType(ResourceType.URL);r1.setUrl("/message*");
		r1.setDescript("信息管理");
		
		Resources r2 = new Resources();
		r2.setName("Message_List"); r2.setPriority(1); r2.setResourceType(ResourceType.URL);r2.setUrl("/messageList");
		r2.setParent(r1);r2.setDescript("信息查看");
		
		Resources r3 = new Resources();
		r3.setName("Message_Post"); r3.setPriority(2); r3.setResourceType(ResourceType.URL);r3.setUrl("/messagePost");
		r3.setParent(r1);r3.setDescript("信息提交");
		
		Resources r4 = new Resources();
		r4.setName("Message_Delete"); r4.setPriority(3); r4.setResourceType(ResourceType.URL);r4.setUrl("/messageDelete");
		r4.setParent(r1); r4.setDescript("信息删除");

		resources.add(r1);
		resources.add(r2);
		resources.add(r3);
		resources.add(r4);
		
		//create role
		Role adminRole = new Role();
		adminRole.setName("ROLE_USER");
		adminRole.setResources(resources);
		roleRepository.save(adminRole);
		
		//create user
		User admin = new User();
		admin.setUserName("admin");
		
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		admin.setPassword(encoder.encodePassword("password", admin.getUserName()));
		admin.setRole(adminRole);
		
		userRepository.save(admin);
		
		
		List<Resources> resources2 = new ArrayList<Resources>();
		Resources rs1 = new Resources();
		rs1.setName("Json_do1"); rs1.setPriority(1); rs1.setResourceType(ResourceType.URL);rs1.setUrl("/json/do1");
		Resources rs2 = new Resources();
		rs2.setName("Json_do2"); rs2.setPriority(2); rs2.setResourceType(ResourceType.URL);rs2.setUrl("/json/do2");
		Resources rs3 = new Resources();
		rs3.setName("Json_do3"); rs3.setPriority(3); rs3.setResourceType(ResourceType.URL);rs3.setUrl("/json/do3");
		resources2.add(rs1);
		resources2.add(rs2);
		resources2.add(rs3);
		
		Role userRole = new Role();
		userRole.setName("user");
		userRole.setResources(resources2);
		
		roleRepository.save(userRole);
		
		User user = new User();
		user.setUserName("user");
		user.setPassword(encoder.encodePassword("password", user.getUserName()));
		userRepository.save(user);
	}
}
